<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;

use Config , Log;

class WebController extends Controller {

    protected $_response;

    public function __construct()
    {
        $this->_response                            = array();
        $this->_response['_title']                  = '小一农货';

    }


    public function getAmapStore($longitude , $latitude){
        $apamParam = '';
        $apamParam .= 'key='.Config::get('amap.key').'&';
        $apamParam .= 'tableid='.Config::get('amap.tableid').'&';
        $apamParam .= 'center='.$longitude.','.$latitude.'&';
        $apamParam .= 'radius=2000';

        //获取周边信息
        $url = "http://yuntuapi.amap.com/datasearch/around?".$apamParam;

        $amapData = json_decode( curlGet($url) );

        Log::debug(json_encode($amapData));

        if($amapData->info != 'OK'){
            return response()->json([ 'code' => -2 , 'mag' => '获取店铺信息失败,请重试' ] );
        }

        $storeId = 0;
        $mindistance = 0;

        foreach ($amapData->datas as $ad){
            if($mindistance == 0){
                $mindistance = $ad->_distance;
                $storeId = $ad->store_id;
            }else{
                if($ad->_distance < $mindistance){
                    $mindistance = $ad->_distance;
                    $storeId = $ad->store_id;
                }
            }
        }

        return $storeId;
    }

}